<?php

include_once "Controller.php";
include_once "models/Artist.php";
include_once "lib/WebServices/MusicBrainz.php";

class Artist extends Controller {
  
  public function Artist($attr=null){ 
    $this->model = new Artist_($attr);
  }
  /*
   * @return array of mysql objects
   * containing artist info, tracks ids,urls, times_added, total_played
   * release title
   */
  public function getProductionList(){
    $this->result = $this->model()->getProductionList();
    return $this;
  }
  
  public function getPicture(){
  	if($this->result()->picture != null){      
      return;
    }
    // get artist picture from last.fm
    $mb = new MusicBrainz(8);
    $mb->setMethod('artist_img');
    try{
      $a_img = $mb->search($this->result()->name)->getArtistImg();
      if(stristr($a_img,'noimage/')) $a_img = null; 
      $image_searched = true;
    }
    catch(MTException $e){ echo('WARNING: '.$e->getMessage()); }
    
    if(isset($image_searched))
      $this->update(array(picture=>($a_img?$a_img:'NO_PICTURE')));
  
  }
  public function getReleases(){
    $this->result = $this->model()->getReleases();
    return $this;	
  }
  
  public function releases(){
  
    $artist_releases = array();
    $release_tracks = array();
    $release_id = -1;
    $count = 0;
    $total = $this->result()->numRows();
    
    while($res=$this->result()->fetchObject()){
       
      $diff_rel = ($res->release_id != $release_id);
      
      
      if($diff_rel && $count){
        array_push($artist_releases,$release_tracks);
        $release_tracks = array();
      }
        
      array_push($release_tracks,$res);
      
      if($count == $total-1)
         array_push($artist_releases,$release_tracks);
      
      $release_id = $res->release_id; 
      $count++;
    }
    
    return $artist_releases;
    
  }
}

?>
